package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ModuleArchivesDao;
import com.sugon.dao.ModuleTypePositionDao;
import com.sugon.entity.ModuleArchivesEntity;
import com.sugon.entity.MoldTypePositionEntity;
import com.sugon.service.ModuleTypePositionService;
import com.sugon.utils.Constant;
import com.sugon.utils.excel.ExcelImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 模具位置关联Service实现类
 *
 * @author YuXD
 * @date 2023-02-23
 */
@Service("moduleTypePositionService")
public class ModuleTypePositionServiceImpl implements ModuleTypePositionService {

    @Autowired
    private ModuleTypePositionDao moldTypePositionDao;
    @Autowired
    private ModuleArchivesDao moduleArchivesDao;

    @Override
    public MoldTypePositionEntity queryObject(String id) {
        return moldTypePositionDao.queryObject(id);
    }

    @Override
    public List<MoldTypePositionEntity> queryList(Map<String, Object> map) {
        return moldTypePositionDao.queryList(map);
    }

    @Override
    public List<MoldTypePositionEntity> queryList(String moldType) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("moldTypeId", moldType);
        return queryList(param);
    }

    @Override
    public List<MoldTypePositionEntity> queryList(String moldTypeId, String positionName, String positionNo) {
        Map<String, Object> param = MapUtil.newHashMap();
        param.put("moldTypeId", moldTypeId);
        param.put("positionName", positionName);
        param.put("positionNo", positionNo);
        return queryList(param);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return moldTypePositionDao.queryTotal(map);
    }

    @Override
    public int save(MoldTypePositionEntity modulPositionCorrelation) {
        modulPositionCorrelation.setId(RandomUtil.randomString(32));
        modulPositionCorrelation.setCreateTime(DateUtil.date());
        modulPositionCorrelation.setDelFlag(Constant.IS_NOT_DEL_INT);
        return moldTypePositionDao.save(modulPositionCorrelation);
    }

    @Override
    public int update(MoldTypePositionEntity modulPositionCorrelation) {
        modulPositionCorrelation.setUpdateTime(DateUtil.date());
        modulPositionCorrelation.setUpdateUser(getUserId());
        return moldTypePositionDao.update(modulPositionCorrelation);
    }

    @Override
    public int delete(String id) {
        return moldTypePositionDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return moldTypePositionDao.deleteBatch(ids);
    }

    @Override
    public int importE(MultipartFile file) {
        // 创建模具键值对，以模具号为key,模具编码为value
        Map<String, String> modules = new HashMap<>();
        List<ModuleArchivesEntity> moduleArchivesList = moduleArchivesDao.queryList(null);
        moduleArchivesList.forEach(module -> {
            modules.put(module.getModuleNo(), module.getModuleCode());
        });
        //获取所有表格数据
        List<String[]> list = ExcelImport.getExcelData(file);
        int count = 0;
        if (list != null && list.size() != 0) {
            for (int i = 1; i < list.size(); i++) {
                boolean isExist = true;
                String[] array = ArrayUtil.resize(list.get(i), 9);
                // 模具编码必填
                if (StrUtil.isEmpty(array[0])) {
                    continue;
                }
                // 模具位置必填
                if (StrUtil.isEmpty(array[1])) {
                    continue;
                }
                // 位置号必填
                if (StrUtil.isEmpty(array[2])) {
                    continue;
                }
                // 寿命标准必填
                if (StrUtil.isEmpty(array[5])) {
                    continue;
                }
                // 首先根据模具号+备件名称+位置号，唯一查询模具关联位置是否存在，如果存在则更新，否则新增
                //List<MoldTypePositionEntity> mpcList = moldTypePositionDao.queryByCondition(array[0], array[1], array[2]);
                //MoldTypePositionEntity mpc;
                //if (CollectionUtil.isNotEmpty(mpcList)) {
                //    mpc = mpcList.get(0);
                //} else {
                //    isExist = false;
                //    mpc = new MoldTypePositionEntity();
                //}
                MoldTypePositionEntity mpc = new MoldTypePositionEntity();
                mpc.setPositionName(array[1]);
                mpc.setPositionNo(array[2]);
                // 规格
                if (StrUtil.isNotEmpty(array[3])) {
                    mpc.setSpec(array[3]);
                }
                // 类型
                if (StrUtil.isNotEmpty(array[4])) {
                    mpc.setModType(array[4]);
                }
                // 设置寿命标准
                mpc.setLifeStandard(Integer.parseInt(array[5]));
                // 特殊处理
                mpc.setPositionRemark(array[6]);
                // 适用型芯条码规格列表
                if (StrUtil.isNotEmpty(array[8])) {
                    mpc.setBarCodeRange(array[8]);
                }
                count += isExist ? update(mpc) : save(mpc);
            }
        }
        return count;
    }
}
